home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Clip Drag and Drop / ClipViewAll / ClipViewAll.cs next >
Encoding:
Text File  |  2001-01-15  |  5.9 KB  |  178 lines

  1. //------------------------------------------
  2. // ClipViewAll.cs ⌐ 2001 by Charles Petzold
  3. //------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Drawing.Imaging;
  7. using System.IO;
  8. using System.Windows.Forms;
  9.  
  10. class ClipViewAll: Form
  11. {
  12.      Panel       panelDisplay, panelButtons;
  13.      RadioButton radioChecked;
  14.      string[]    astrFormatsSave = new string[0];
  15.  
  16.      public static void Main()
  17.      {
  18.           Application.Run(new ClipViewAll());
  19.      }
  20.      public ClipViewAll()
  21.      {
  22.           Text = "Clipboard Viewer (All Formats)";
  23.  
  24.                // Create variable-width panel for clipboard display.
  25.  
  26.           panelDisplay = new Panel();
  27.           panelDisplay.Parent = this;
  28.           panelDisplay.Dock = DockStyle.Fill;
  29.           panelDisplay.Paint += new PaintEventHandler(PanelOnPaint);
  30.           panelDisplay.BorderStyle = BorderStyle.Fixed3D;
  31.  
  32.                // Create splitter.
  33.  
  34.           Splitter split = new Splitter();
  35.           split.Parent = this;
  36.           split.Dock = DockStyle.Left;
  37.  
  38.                // Create panel for radio buttons.
  39.  
  40.           panelButtons = new Panel();
  41.           panelButtons.Parent = this;
  42.           panelButtons.Dock = DockStyle.Left;
  43.           panelButtons.AutoScroll = true;
  44.           panelButtons.Width = Width / 2;
  45.  
  46.                // Set time for 1 second.
  47.  
  48.           Timer timer = new Timer();
  49.           timer.Interval = 1000;
  50.           timer.Tick += new EventHandler(TimerOnTick);
  51.           timer.Enabled = true;
  52.      }
  53.      void TimerOnTick(object obj, EventArgs ea)
  54.      {
  55.           IDataObject data = Clipboard.GetDataObject();
  56.  
  57.           string[] astrFormats = data.GetFormats();
  58.           bool bUpdate = false;
  59.  
  60.                // Determine whether clipboard formats have changed.
  61.  
  62.           if (astrFormats.Length != astrFormatsSave.Length)
  63.                bUpdate = true;
  64.           else
  65.           {
  66.                for (int i = 0; i < astrFormats.Length; i++)
  67.                     if (astrFormats[i] != astrFormatsSave[i])
  68.                     {
  69.                          bUpdate = true;
  70.                          break;
  71.                     }
  72.           }
  73.                // Invalidate display regardless.
  74.  
  75.           panelDisplay.Invalidate();
  76.  
  77.                // Don't update buttons if formats haven't changed.
  78.  
  79.           if (!bUpdate)
  80.                return;
  81.  
  82.                // Formats have changed, so re-create radio buttons.
  83.  
  84.           astrFormatsSave = astrFormats;
  85.           panelButtons.Controls.Clear();
  86.           Graphics grfx = CreateGraphics();
  87.           EventHandler eh = new EventHandler(RadioButtonOnClick);
  88.           int cxText = AutoScaleBaseSize.Width;
  89.           int cyText = AutoScaleBaseSize.Height;
  90.  
  91.           for (int i = 0; i < astrFormats.Length; i++)
  92.           {
  93.                RadioButton radio = new RadioButton();
  94.                radio.Parent = panelButtons;
  95.                radio.Text = astrFormats[i];
  96.  
  97.                if (!data.GetDataPresent(astrFormats[i], false))
  98.                     radio.Text += "*";
  99.  
  100.                try
  101.                {
  102.                     object objClip = data.GetData(astrFormats[i]);
  103.                     radio.Text += " (" + objClip.GetType() + ")";
  104.                }
  105.                catch
  106.                {
  107.                     radio.Text += " (Exception on GetData or GetType!)";
  108.                }
  109.                radio.Tag = astrFormats[i];
  110.                radio.Location = new Point(cxText, i * 3 * cyText / 2);
  111.                radio.Size = new Size((radio.Text.Length + 20) * cxText, 
  112.                                      3 * cyText / 2);
  113.                radio.Click += eh;
  114.           }
  115.           grfx.Dispose();
  116.           radioChecked = null;
  117.      }
  118.      void RadioButtonOnClick(object obj, EventArgs ea)
  119.      {
  120.           radioChecked = (RadioButton) obj;
  121.           panelDisplay.Invalidate();
  122.      }
  123.      void PanelOnPaint(object obj, PaintEventArgs pea)
  124.      {
  125.           Panel    panel = (Panel) obj;
  126.           Graphics grfx  = pea.Graphics;
  127.           Brush    brush = new SolidBrush(panel.ForeColor);
  128.  
  129.           if (radioChecked == null)
  130.                return;
  131.  
  132.           IDataObject data = Clipboard.GetDataObject();
  133.           object objClip = data.GetData((string) radioChecked.Tag);
  134.  
  135.           if (objClip == null)
  136.                return;
  137.  
  138.           else if (objClip.GetType() == typeof(string))
  139.           {
  140.                grfx.DrawString((string)objClip, Font, brush, 
  141.                                panel.ClientRectangle);
  142.           }
  143.           else if (objClip.GetType() == typeof(string[]))   // FileDrop
  144.           {
  145.                string str = string.Join("\r\n", (string[]) objClip);
  146.  
  147.                grfx.DrawString(str, Font, brush, panel.ClientRectangle);
  148.           }
  149.           else if (objClip.GetType() == typeof(Bitmap) ||
  150.                    objClip.GetType() == typeof(Metafile) ||
  151.                    objClip.GetType() == typeof(Image))
  152.           {
  153.                grfx.DrawImage((Image)objClip, 0, 0);
  154.           }
  155.           else if (objClip.GetType() == typeof(MemoryStream))
  156.           {
  157.                Stream stream = (Stream) objClip;
  158.                byte[] abyBuffer = new byte[16];
  159.                long   lAddress = 0;
  160.                int    iCount;
  161.                Font   font = new Font(FontFamily.GenericMonospace, 
  162.                                       Font.SizeInPoints);
  163.                float  y = 0;
  164.  
  165.                while ((iCount = stream.Read(abyBuffer, 0, 16)) > 0)
  166.                {
  167.                     string str = HexDump.ComposeLine(lAddress, abyBuffer, 
  168.                                                                iCount);
  169.                     grfx.DrawString(str, font, brush, 0, y);
  170.                     lAddress += 16;
  171.                     y += font.GetHeight(grfx);
  172.  
  173.                     if (y > panel.Bottom)
  174.                          break;
  175.                }
  176.           }
  177.      }
  178. }